import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { ApplyWareService } from './apply-ware.service';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-apply-ware',
  templateUrl: './apply-ware.component.html',
  styleUrls: ['./apply-ware.component.scss']
})
export class ApplyWareComponent implements OnInit {
  //#region 搜索
  ApplyCode: string = "";
  ApplyUser: string = "";
  ExState: any = "";
  billType: any = "";
  searchType: any = "";
  DocumentType: any = "";
  delList = [];
  sort="reqDate,desc";
  //#endregion
  constructor(
    private applyWareService: ApplyWareService,
    private modal: NzModalService,
    private router: Router,
  ) { }
  ngOnInit() {
    this.GetSearchPageList();
    this.GetEnumWareInState();
  }
  //#region 搜索下拉
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 11:20:27
   * @Descripttion: 状态
   * @Return: 
   */
  selectedExState(data) {
    this.GetSearchPageList();
  }

  selectedDocumentCategories(data) {
    this.GetSearchPageList();
  }

  selectedDocumentType(data) {
    this.GetSearchPageList();
  }
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 11:05:27
   * @Descripttion: 类别【出入库】 
   * @Return: 
   */
  selectedType(data) {
    this.DocumentType = "";

    this.GetMaterialInBillTypeEnum(data);

    this.GetSearchPageList();
  }


  //#endregion

  //#region 按钮，数据
  editDisabled: boolean = true;
  deleteDisabled: boolean = true;
  infoDisabled: boolean = true;
  listData = [];
  //#endregion

  isAllChecked: boolean = false;
  checkAll(value: boolean): void {
    this.listData.forEach(item => {
      item.checked = value;
    });
    if (value == true) {
      var count = this.listData.filter(s => s.checked);
      if (count.length != 0) {
        if (count.length == 1) {
          let model = count[0].status;
          if (model == 3 || model==4) {
            this.editDisabled = false;
          }
        }

        this.deleteDisabled = false;
        this.infoDisabled = false;
      }
      if (count.length != 1) {
        this.editDisabled = true;
        this.infoDisabled = true;
      }
    }
    else {
      this.editDisabled = true;
      this.deleteDisabled = true;
      this.infoDisabled = true;
    }
  }
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 11:04:28
   * @Descripttion: 
   * @Return: 
   */
  selId: string = "";
  type: number = 0;
  SelectRow(): void {
    let _isAllCheck = true;
    this.listData.forEach(item => {
      if (!item.checked) {
        _isAllCheck = false;
      }
    })
    this.isAllChecked = _isAllCheck;
    var checkCount = this.listData.filter(item => item.checked).length;
    // 判断能否编辑或删除
    checkCount > 0 ? this.deleteDisabled = false : this.deleteDisabled = true;
    if (checkCount === 1) {
      this.delList = this.listData.filter(s => s.checked);
      //下面为测试注掉
      if (this.delList[0].status == 3 || this.delList[0].status == 4) {
        this.editDisabled = false;
      }
      //this.editDisabled = false;
      this.infoDisabled = false;
      if (this.delList.length != 0) {
        this.selId = this.delList[0].id;
        this.type = this.delList[0].type;
      }
    }
    else {
      this.editDisabled = true;
      this.infoDisabled = true;
    }
    //checkCount === 1 ? this.editDisabled = false : this.editDisabled = true;
  }

  //#region 选择、分页
  pageIndex: any = 1;//提交的页码
  pagesize: any = 10;//table每页条数
  totalSize: any = '';//总共条数

  clickPage(pageIndex) {
    this.pageIndex = pageIndex;
    this.GetSearchPageList();
  }
  clickSize(pageSize) {
    this.pagesize = pageSize
    this.GetSearchPageList();
  }
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 11:04:21
   * @Descripttion: 
   * @Return: 
   */
  //#endregion
  GetSearchPageList() {
    let data = {
      billType: this.DocumentType,
      code: this.ApplyCode,
      inoutStatus: "",
      nickName: this.ApplyUser,
      status: this.ExState,
      type: this.searchType,
      page: this.pageIndex - 1,
      size: this.pagesize,
      sort:this.sort
    }
    console.log(data);
    this.applyWareService.apiList(data).subscribe(res => {
      res.content.forEach(s => s.checked == false);
      this.listData = res.content
      this.totalSize = res.totalElements
    });
  }
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 11:04:17
   * @Descripttion: 
   * @Return: 
   */
  Del() {
    let delList: any = this.listData.filter(s => s.checked == true);
    if (delList.length == 0) {
      this.modal.warning({
        nzTitle: "删除提示",
        nzContent: "请选择要删除的数据"
      })
      return false;
    }
    else {
      var ids = [];
      delList.forEach(e => {
        ids.push(e.id);
      });
      this.modal.confirm({
        nzTitle: "删除提示",
        nzContent: "确定要删除这条数据吗？",
        nzOnOk: () => {
          this.DelMethod(ids);
        }
      })
    }
  }
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 09:49:03
   * @Descripttion: 
   * @Return: 
   */
  private DelMethod(ids) {
    this.applyWareService.del(ids).subscribe(res => {
      this.GetSearchPageList();
      this.editDisabled = true;
      this.deleteDisabled = true;
      this.infoDisabled = true;
    })
  }
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 09:22:39
   * @Descripttion: 点击修改 
   * @Return: 
   */
  Edit() {
    if (this.type == 1) {
      this.router.navigate(['/material/warehouse/applywarewarehousing', this.selId]);
    }
    else if (this.type == 2) {
      this.router.navigate(['/material/warehouse/applywareoutofstock', this.selId]);
    }
  }
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 09:22:15
   * @Descripttion: 点击查看详情 
   * @Return: 
   */
  info() {
    if (this.type == 1) {
      this.router.navigate(['/material/warehouse/applywarewarehousingdetail', this.selId]);
    }
    else if (this.type == 2) {
      this.router.navigate(['/material/warehouse/applywareoutofstockdetail', this.selId]);
    }
  }
  /**
   * @Author: 闫聪敏
   * @Email: 
   * @Date: 2020-03-06 11:07:45
   * @Descripttion: 
   * @Return: 
   */
  wareInStateList = [];
  GetEnumWareInState() {
    let data = {
      enumName: "MaterialInoutStatusEnum"
    }
    this.applyWareService.GetEnum(data).subscribe(res => {
      for (const key in res) {
        var e = { key: key, value: res[key] }
        this.wareInStateList.push(e);
      }
      console.log(this.wareInStateList);
    });
  }

  MaterialInBillTypeEnum = [];
  GetMaterialInBillTypeEnum(type) {
    let MaterialInBillTypeEnumPri = [];
    let data = {};
    if (type == "") {
      this.MaterialInBillTypeEnum = [];
      return;
    }
    if (type == 1) {
      data = {
        enumName: "MaterialInBillTypeEnum"
      }
    }
    else if (type == 2) {
      data = {
        enumName: "MaterialOutBillTypeEnum"
      }
    }
    this.applyWareService.GetEnum(data).subscribe(res => {
      for (const key in res) {
        var e = { key: key, value: res[key] }
        MaterialInBillTypeEnumPri.push(e);
      }
      this.MaterialInBillTypeEnum = MaterialInBillTypeEnumPri
      console.log(this.MaterialInBillTypeEnum);
    });
  }
}
